package com.vitaf.cash.app.utils.helper

import com.vitaf.cash.app.utils.VitafDateUtil
import com.vitaf.cash.app.utils.shared.VitafSharedHp
import com.vitaf.cash.app.utils.shared.VitafSharedKey
import org.json.JSONObject
import java.util.Calendar

/**
 * ProcessHelper
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:44
 * @Annotation：用户流程状态 帮助类
 */
object VitafProcessHp {

    /**
     * 保存用户流程数据
     */
    fun saveGombeCreditProcessData(data: JSONObject){
        VitafSharedHp.addGombeCreditSharedData(VitafSharedKey.GOMBECREDIT_PROCESS, data.toString())
    }

    /**
     * 获取流程数据
     */
    fun takeGombeCreditProcessData(): JSONObject? {
        val dataString = VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_PROCESS)
        if (dataString.isEmpty()){
            return null
        }
        return JSONObject(dataString)
    }

    /**
     * 获取所有流程titleList
     */
    fun takeGombeCreditAllStepTitleList(): ArrayList<String> {
        val titleList = arrayListOf<String>()
        val data = takeGombeCreditProcessData() ?: return titleList

        val allStepTitles = data.optJSONArray("allStepTitles")
        if (allStepTitles != null) {
            for (i in 0..<allStepTitles.length()){
                titleList.add(allStepTitles.optString(i))
            }
        }
        return titleList
    }

    /**
     * 获取总流程步骤key
     */
    fun takeGombeCreditAllStepList(): ArrayList<String>{
        val allStepList = arrayListOf<String>()
        val data = takeGombeCreditProcessData() ?: return allStepList
        val allSteps = data.optJSONArray("allSteps")
        if (allSteps != null){
            for (i in 0..<allSteps.length()){
                allStepList.add(allSteps.optString(i))
            }
        }
        return allStepList
    }

    /**
     * 获取剩余未完成的流程列表
     */
    fun takeGombeCreditStepList(): ArrayList<String>{
        val stepList = arrayListOf<String>()
        val data = takeGombeCreditProcessData() ?: return stepList
        val steps = data.optJSONArray("steps")
        if (steps != null){
            for (i in 0..<steps.length()){
                stepList.add(steps.optString(i))
            }
        }
        return stepList
    }

    /**
     * 获取获取当前进行的步骤下标
     */
    fun takeGombeCreditStepIndex(): Int{
        var stepIndex = takeGombeCreditAllStepList().size
        if (takeGombeCreditStepList().size == 0){
            //所有步骤已完成
            return stepIndex
        }
        val curtStep = takeGombeCreditStepList()[0]
        for (i in 0..<stepIndex){
            if (curtStep == takeGombeCreditAllStepList()[i]){
                stepIndex = i
                break
            }
        }
        return stepIndex
    }

    /**
     * 获取订单状态
     */
    fun takeGombeCreditOrderStatus(): String{
        val data = takeGombeCreditProcessData() ?: return ""
        return data.optString("orderStatus","")
    }

    /**
     * 获取申请状态
     */
    fun takeGombeCreditApplyStatus(): String{
        val data = takeGombeCreditProcessData() ?: return ""
        return data.optString("applyStatus","")
    }

    /**
     * 授信状态
     */
    fun takeGombeCreditBeforeRiskStatus(): String{
        val data = takeGombeCreditProcessData() ?: return ""
        return data.optString("beforeRiskStatus","")
    }

    /**
     * 是否是可以修改手机号
     */
    fun isGombeCreditMobileEditFlag(): Boolean{
        val data = takeGombeCreditProcessData() ?: return false
        return data.optString("mobileEditFlag") == "YES"
    }

    /**
     * 是否开放WhatsApp手机校验方式  YES、NO
     */
    fun isGombeCreditCheckWhatsApp(): Boolean{
        val data = takeGombeCreditProcessData() ?: return false
        return data.optString("whatsappFlag") == "YES"
    }

    /**
     * 是否开放SMS 手机校验方式  YES、NO
     */
    fun isGombeCreditCheckSMS(): Boolean{
        val data = takeGombeCreditProcessData() ?: return false
        return data.optString("smsFlag") == "YES"
    }

    /**
     * 获取可再次申请时间
     */
    fun takeGombeCreditApplyAgainDate(): String{
        val data = takeGombeCreditProcessData() ?: return ""
        return data.optString("againApplyDate")
    }

    /**
     * 获取授信到期时间
     */
    fun takeGombeCreditExpireDate(): String{
        val data = takeGombeCreditProcessData() ?: return ""
        if (!data.has("creditExpireDate")){
            val calendar = Calendar.getInstance()
            calendar.add(Calendar.YEAR,1)
            return VitafDateUtil.getFormatTime(calendar.timeInMillis,"MM/dd/yyyy")
        }
        return data.optString("creditExpireDate")
    }

    /**
     * 流程接口状态： 审核中
     */
    fun isGombeCreditReviewing(): Boolean{
        val applyStatus = takeGombeCreditApplyStatus()
        return when(applyStatus){
            "CREATED",              //已创建
            "SYS_CHECK",            //审核中
            "SYS_APROVAL",          //机审通过
            "MANU_APROVAL",         //人审通过
            "DS_APROVAL" -> true    //电审通过
            else -> false
        }
    }

    /**
     * 流程接口状态: 放款中
     */
    fun isGombeCreditDisbursing(): Boolean{
        val orderStatus = takeGombeCreditOrderStatus()
        return when(orderStatus){
            "CREATED",              //已创建
            "LENDING" -> true       //放款中
            else -> false
        }
    }

    /**
     * 流程接口状态: 被拒绝
     */
    fun isGombeCreditRejected(): Boolean{
        val applyStatus = takeGombeCreditApplyStatus()
        //申请被拒绝
        val isApplyRejected = when(applyStatus){
            "SYS_DENIED",       //已拒绝
            "MANU_DENIED",      //人审拒绝
            "DS_DENIED" -> true //电审拒绝
            else -> false
        }
        if (isApplyRejected){
            return true
        }
        val orderStatus = takeGombeCreditOrderStatus()
        //订单状态被拒绝
        return when(orderStatus){
            "DENIED" -> true    //已拒绝
            else -> false
        }
    }

    /**
     * 流程接口状态: 待还款
     */
    fun isGombeCreditRepay(): Boolean{
        val orderStatus = takeGombeCreditOrderStatus()
        return when(orderStatus){
            "LOANED",   //待还款（未逾期）
            "OVERDUE",  //待还款（已逾期
            "BAD_DEBT" -> true  // 坏账
            else -> false
        }
    }
}